package com.hengtiansoft.lfy.fragment;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hengtiansoft.lfy.R;
import com.hengtiansoft.lfy.activity.AboutActivity;
import com.hengtiansoft.lfy.activity.CollectionActivity;
import com.hengtiansoft.lfy.activity.ExerciseCompletedActivity;
import com.hengtiansoft.lfy.activity.LfySettingActivity;

import com.hengtiansoft.lfy.activity.LoginActivityV2;
import com.hengtiansoft.lfy.activity.MyInformationActivity;
import com.hengtiansoft.lfy.base.BaseActivity;
import com.hengtiansoft.lfy.base.BaseFragment;

import com.hengtiansoft.lfy.chart.applib.utils.HXPreferenceUtils;
import com.hengtiansoft.lfy.constants.Constants;
import com.hengtiansoft.lfy.entity.EvaluatorRankings;
import com.hengtiansoft.lfy.net.NetHelper;
import com.hengtiansoft.lfy.net.NetUtils;
import com.hengtiansoft.lfy.utils.CircleImageView;
import com.hengtiansoft.lfy.utils.Config;
import com.hengtiansoft.lfy.utils.DataCleanManager;
import com.hengtiansoft.lfy.widget.CodeDialog;
import com.hengtiansoft.lfy.widget.ColorArcProgressBar;
import com.squareup.picasso.Picasso;

/**
 * 我的fragment
 * daiye
 */
public class MyFragment extends BaseFragment implements View.OnClickListener {
    private ImageView barcode;
    Context mContext;
    private CircleImageView iv_headicon;
    private TextView tv_username;
    private RelativeLayout layout_collection;
    private RelativeLayout layout_settings;
    private RelativeLayout layout_about;
    private RelativeLayout layout_clear;
    private String datasize = "0M";
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_my, container, false);
        initView(v);
        return v;
    }

    private void initView(View v) {
        barcode = (ImageView) v.findViewById(R.id.barcode);
        barcode.setOnClickListener(this);
        iv_headicon = (CircleImageView) v.findViewById(R.id.iv_headicon);
        iv_headicon.setOnClickListener(this);
        tv_username = (TextView) v.findViewById(R.id.tv_username);
        tv_username.setOnClickListener(this);
        layout_collection = (RelativeLayout) v.findViewById(R.id.layout_collection);
        layout_collection.setOnClickListener(this);
        layout_settings = (RelativeLayout) v.findViewById(R.id.layout_settings);
        layout_settings.setOnClickListener(this);
        layout_about = (RelativeLayout) v.findViewById(R.id.layout_about);
        layout_about.setOnClickListener(this);
        layout_clear= (RelativeLayout) v.findViewById(R.id.layout_clear);
        layout_clear.setOnClickListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        if(!isHidden())
            updataView();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if(!hidden){
            updataView();
        }
    }

    /**
     * 更新各种数值
     */
    private void updataView() {
        try {
            datasize = DataCleanManager
                    .getFormatSize(DataCleanManager.getCacheSize(getActivity()));
        } catch (Exception e) {

        }
        if(Config.isUserLogined()){
            ((BaseActivity)getActivity()).setStatusBar(R.color.white);
            if(!TextUtils.isEmpty(HXPreferenceUtils.getInstance().getCurrentUserNick())){
                tv_username.setText(HXPreferenceUtils.getInstance().getCurrentUserNick());
            }else{
                tv_username.setText(getString(R.string.no_data));
            }
            setCurrentUserAvatar(HXPreferenceUtils.getInstance().getCurrentUserAvatar(),self,iv_headicon);
//            if (null != HXPreferenceUtils.getInstance().getCurrentUserSex()) {
//
//                if (HXPreferenceUtils.getInstance().getCurrentUserSex().equals("m")) {
//                    Log.i("supeng","m");
//                    Drawable drawableMan = getResources().getDrawable(R.drawable.me_sex);
//                    drawableMan.setBounds(0, 0, drawableMan.getMinimumWidth(), drawableMan.getMinimumHeight());
//                    tv_username.setCompoundDrawables(null, null, drawableMan, null);
//                } else if (HXPreferenceUtils.getInstance().getCurrentUserSex()
//                        .equals("f")) {
//                    Log.i("supeng","f");
//                    Drawable drawableGirl = getResources().getDrawable(R.drawable.sex_girl);
//                    drawableGirl.setBounds(0, 0, drawableGirl.getMinimumWidth(), drawableGirl.getMinimumHeight());
//                    tv_username.setCompoundDrawables(null, null, drawableGirl, null);
//                }
//            }else{
//                tv_username.setCompoundDrawables(null, null, null, null);
//            }
        }else{
            Picasso.with(self).load(R.drawable.img_head)
                    .into(iv_headicon);
            ((BaseActivity)getActivity()).setStatusBar(R.color.line_color);
            tv_username.setText(getString(R.string.login_user_not_logged_tips));


        }
    }
    /**
     * 设置当前用户头像
     */
    public static void setCurrentUserAvatar(String url,Context context,
                                            ImageView imageView) {
        if (!TextUtils.isEmpty(url)) {
            if(url.indexOf("http")==-1) {
                Picasso.with(context)
                        .load(Constants.GETIMAGE + url)
                        .placeholder(R.drawable.img_head).into(imageView);
            }else{
                Picasso.with(context)
                        .load(url)
                        .placeholder(R.drawable.img_head).into(imageView);
            }
        } else {
            Picasso.with(context).load(R.drawable.img_head)
                    .into(imageView);
        }
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case  R.id.barcode:
                if (Config.isUserLogined()) {
                    // 二维码
                    final CodeDialog codeDialog = new CodeDialog(getContext(),
                            Config.getUserName(),
                            HXPreferenceUtils.getInstance().getCurrentUserNick(),
                            HXPreferenceUtils.getInstance().getCurrentUserSex());
                    codeDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                    codeDialog.show();
                } else {
                    startActivity(new Intent(getActivity(), LoginActivityV2.class));
                    Toast.makeText(getActivity(), getString(R.string.not_login_title), Toast.LENGTH_LONG)
                            .show();
                }
                break;
            case R.id.iv_headicon:
                if (Config.isUserLogined()) {
                    startActivity(new Intent(getActivity(), MyInformationActivity.class));
                } else {
                    startActivity(new Intent(getActivity(), LoginActivityV2.class));
                    Toast.makeText(getActivity(), getString(R.string.not_login_title), Toast.LENGTH_LONG)
                            .show();
                }

                break;
            case R.id.tv_username:
                if (Config.isUserLogined()) {
                    startActivity(new Intent(getActivity(), MyInformationActivity.class));
                } else {
                    startActivity(new Intent(getActivity(), LoginActivityV2.class));
                    Toast.makeText(getActivity(), getString(R.string.not_login_title), Toast.LENGTH_LONG)
                            .show();
                }

                break;
            case R.id.layout_login:
                // test
                startActivity(new Intent(getActivity(), LoginActivityV2.class));

                break;
            case R.id.layout_clear:
                if (!datasize.equals("0.0Byte")) {
                    new AlertDialog.Builder(getActivity()).setTitle(getString(R.string.prompt))
                            .setMessage(getString(R.string.delete_title)).setPositiveButton(getString(R.string.ok),
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    DataCleanManager.cleanApplicationData(getActivity());
                                    DataCleanManager.deleteFolderFile(
                                            Environment.getExternalStorageDirectory()
                                                    + Constants.IMAGE_CACHE_PATH,
                                            true);
                                    DataCleanManager.cleanInternalCache(getActivity());
                                    try {
                                        datasize=DataCleanManager.getFormatSize(DataCleanManager.getCacheSize(getActivity()));
                                    } catch (Exception e) {
                                        datasize=getString(R.string.errorByte);
                                    }
                                    Toast.makeText(getActivity(), getString(R.string.clean_ok), Toast.LENGTH_SHORT).show();
                                }
                            }).setNegativeButton(getString(R.string.cancel), null).show();

                } else {
                    Toast.makeText(getActivity(), getString(R.string.not_clean), Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.layout_finished:
                if(!Config.isUserLogined()){
                    startActivity(new Intent(getActivity(), LoginActivityV2.class)
                            .putExtra("from", "toCollectionActivity"));
                    Toast.makeText(getActivity(),  getString(R.string.not_login_title), Toast.LENGTH_LONG)
                            .show();
                }else
                    startActivity(new Intent(getActivity(), ExerciseCompletedActivity.class));
                break;
            case R.id.layout_collection:
                if (Config.isUserLogined()) {
                    startActivity(
                            new Intent(getActivity(), CollectionActivity.class));
                } else {
                    startActivity(new Intent(getActivity(), LoginActivityV2.class)
                            .putExtra("from", "toCollectionActivity"));
                    Toast.makeText(getActivity(),  getString(R.string.not_login_title), Toast.LENGTH_LONG)
                            .show();
                }
                break;
            case R.id.layout_settings:
                Intent intent = new Intent(getActivity(), LfySettingActivity.class);
                startActivity(intent);
                break;
            case R.id.layout_about:
                startActivity(new Intent(getActivity(), AboutActivity.class));
                break;
        }
    }


}
